<?php
namespace Android\Model;

class TimetableModel extends BaseModel {

    /**
     * 获取指定教学计划课表周数
     *
     * @param $teachplan_id
     * @return integer $weeks
     */
    public function getWeeksByTid ($teachplan_id) {
        $weeks = $this->where('teachplan_id = ' . $teachplan_id)->order('week desc')->limit(1)->getField('week');
        if (empty($weeks))
            return 0;
        return intval($weeks);
    }


    public function getTimetableByWhere($where) {
        return $this->where($where)->select();
    }


    /**
     * 获取指定教学计划周课表
     *
     * @param $teachplan_id
     * @param $week
     * @return mixed
     */
    public function getTimetable ($teachplan_id,$week) {

        $map['teachplan_id'] = $teachplan_id;
        $map['week'] = $week;
        $timetables = $this->where($map)->order('xq asc, start asc')->select();
        return $timetables;

    }


    /**
     * 获取指定教学计划下一次上课信息
     * @param $teachplan_id
     */
    public function getLastTimetable($teachplan_id, $timeInfo) {

        if (empty($timeInfo['week']) || empty($timeInfo['xq'])) return "亲,系统不知道当前时间啦";

        $map['teachplan_id'] = $teachplan_id;
        $map['week'] = array('EGT',$timeInfo['week']);
        $map['xq'] = array('EGT',$timeInfo['xq']);
        $timetable = $this->where($map)->order('week asc,xq asc,start asc')->select();

        //p($timetable);

        if (empty($timetable))
            return "当前教学计划已经没有课程安排啦!";

        foreach ($timetable as $k=>$v) {
            if ($v['week'] == $timeInfo['week'] && $v['xq'] == $timeInfo['xq']) {
                $_jc = getJC();
                if ($_jc < $v['end']) array_splice($timetable,$k,1);
            }
        }

        return "下次上课时间: 第{$timetable[0]['week']}周,星期{$timetable[0]['xq']} {$timetable[0]['start']}~{$timetable[0]['end']}节,{$timetable[0]['address']}";
    }

    /**
     * 获取考勤id
     * @param $teachplan_id
     * @return mixed
     */
    public function getTimeTableId($teachplan_id){
        $rs = $this->table(" __TIMETABLE__ timetable,__TEACHPLAN__ teachplan")
                             ->field("timetable.id")
                             ->where("timetable.teachplan_id = {$teachplan_id}")->select();
        $timetable_id = $rs['0']['id'];
        return $timetable_id;
    }


}